package com.effectiveui.away3d.material{
	import away3d.materials.BitmapMaterial;
	
	import flash.display.BitmapData;
	
	import mx.containers.Canvas;
	import mx.controls.SWFLoader;
	import mx.core.BitmapAsset;
	
	public class MaterialUtil{
		
		public static function getMaterial(value:*, target:Object = null):*{
			var materialBMD:BitmapData;
			if(value is Class){
				var earthObj:BitmapAsset = new value() as BitmapAsset;
				value = new BitmapMaterial(earthObj.bitmapData);
			}else if(value is SWFLoader){
				if(value.width>0 && value.height>0){
					if(materialBMD == null || materialBMD.width != value.contentWidth || materialBMD.height != value.contentHeight){
						materialBMD = new BitmapData(value.contentWidth, value.contentHeight);
					}
					materialBMD.draw(value);
					value = new BitmapMaterial(materialBMD);
				}else{
					value.addEventListener("resize", function():void{ 
						value = MaterialUtil.getMaterial(value);
						if(target){
							target.forceUpdate();
						}
						});
					return value;
				}
			}else if(value is Canvas){
				if(value.width>0 && value.height>0){
					if(materialBMD == null || materialBMD.width != value.width || materialBMD.height != value.height){
						materialBMD = new BitmapData(value.width, value.height);
					}
					materialBMD.draw(value);
					value = new BitmapMaterial(materialBMD);
				}else{
					return;
				}
			}
		}
		
		public static function isValid(value:*):Boolean{
			if(value is String)return true;
			return value!=null&&value.width>0 && value.height>0;
		}
	}
}